package dao;

import entity.Orgnazition;

import java.util.ArrayList;
import java.util.List;

public class OrgnaztitionDao extends BaseDao {
    /**
     * 显示全部
     * @return
     */
    public List<Orgnazition> OrgnaztitionShow() {
        List<Orgnazition> list=new ArrayList<>();
        try {
            conn=super.getConnection();
            String sql="SELECT * FROM `orgnazition`";
            pstmt = conn.prepareStatement(sql);
            rs=pstmt.executeQuery();
            while (rs.next()) {
                Orgnazition orgnazition=new Orgnazition();
                orgnazition.setId(rs.getInt("id"));
                orgnazition.setName(rs.getString("name"));
                orgnazition.setPrincipalName(rs.getString("principalName"));
                orgnazition.setStatus(rs.getInt("status"));
                orgnazition.setCreateDate(rs.getString("createDate"));
                list.add(orgnazition);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            super.closeAll(conn,pstmt,rs);
        }
        return list;
    }
    /**
     * 根据id进行禁用启用
     */
    public int orgnazitionStatus(int id,int status){
        int count=0;
        try {
            //建立连接
            conn=super.getConnection();
            //定义sql
            String sql="UPDATE `orgnazition` SET `status` = ? WHERE `id` = ?";
            //获取执行对象
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,status);
            pstmt.setInt(2,id);
            //执行sql
            count=pstmt.executeUpdate();

        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            super.closeAll(conn,pstmt,rs);
        }
        return count;
    }
    public boolean selectId(String name) {
        boolean i=false;
        try {
            conn=super.getConnection();
            String sql="SELECT * FROM `orgnazition` WHERE name=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,name);
            rs=pstmt.executeQuery();
            if (rs.next()) {
                i=true;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            super.closeAll(conn,pstmt,rs);
        }
        return i;
    }
    public int Add(Orgnazition orgnazition) {
        int count=0;
        try {
            //建立连接
            conn=super.getConnection();
            //定义sql
            String sql="INSERT INTO `orgnazition`( `name`, `principalName`, `status`, `createDate`)" +
                    " VALUES (?,?, 0,?)";
            //获取执行对象
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,orgnazition.getName());
            pstmt.setString(2,orgnazition.getPrincipalName());
            pstmt.setString(3,orgnazition.getCreateDate());
            //执行sql
            count=pstmt.executeUpdate();

        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            super.closeAll(conn,pstmt,rs);
        }
        return count;
    }
}
